﻿#pragma once

#ifndef QCEFCONTEXT_H
#define QCEFCONTEXT_H

#include <QtCore>

#include "QCefDebug.h"
#include "QCefSetting.h"
#include "QCefBrowserSetting.h"
#include "QCefConfig.h"
#include "QCefRenderApp.h"
#include "QCefOtherApp.h"
#include "QCefBrowserApp.h"
#include "QCefWinMsgLoop.h"

class QCefContext : public QObject
{
    Q_OBJECT
public:
    inline static QCefContext * instance() { return ms_instance; }

protected:
    static QCefContext * ms_instance;   //

public:
    explicit QCefContext();
    ~QCefContext();

public:
    QCefSetting & getCefSetting() { return m_cefSetting; }
    QCefBrowserSetting & getCefBrowserSetting() { return m_cefBrowserSetting; }
    QCefConfig & getCefConfig() { return m_cefConfig; }

    CefRefPtr<QCefBrowserApp> getCefBrowserApp() { return m_cefBrowserApp; }
    CefRefPtr<QCefRenderApp> getCefRenderApp() { return m_cefRenderApp; }
    CefRefPtr<QCefOtherApp> getCefOtherApp() { return m_cefOtherApp; }
    QCefWinMsgLoop * getWinMsgLoop() { return m_msgLoop; }

public:
    int init();
    int fini();

protected:
    QCefSetting                 m_cefSetting;
    QCefBrowserSetting          m_cefBrowserSetting;
    QCefConfig                  m_cefConfig;

    CefRefPtr<QCefBrowserApp>   m_cefBrowserApp;
    CefRefPtr<QCefRenderApp>    m_cefRenderApp;
    CefRefPtr<QCefOtherApp>     m_cefOtherApp;

    QTimer *                    m_timer;
    QCefWinMsgLoop *            m_msgLoop;    

protected slots:
    void onAboutToQuit();
    void onMessageLoop();
};

inline QCefContext *        cefCtxPtr() { return (QCefContext::instance()); }
inline QCefContext &        cefCtx() { return *(QCefContext::instance()); }

inline QCefSetting &        cefSetting() { return  QCefContext::instance()->getCefSetting(); }
inline CefSettings &        cefSettings() { return QCefContext::instance()->getCefSetting().getCefSettings(); }

inline QCefBrowserSetting & cefBrowserSetting() { return  QCefContext::instance()->getCefBrowserSetting(); }
inline CefBrowserSettings & cefBrowserSettings() { return QCefContext::instance()->getCefBrowserSetting().getCefBrowserSettings(); }

#endif // QCEFCONTEXT_H
